let timer;
let isRunning = false;
let isBreak = false;
let sessionCount = 0;
let workDuration = 25 * 60;
let shortBreakDuration = 5 * 60;
let longBreakDuration = 15 * 60;
let currentTime = workDuration;

const timeDisplay = document.getElementById('time-display');
const startPauseButton = document.getElementById('start-pause');
const resetButton = document.getElementById('reset');
const sessionCountDisplay = document.getElementById('session-count');
const longBreakButton = document.getElementById('long-break');
const workDurationInput = document.getElementById('work-duration');
const shortBreakDurationInput = document.getElementById('short-break-duration');
const longBreakDurationInput = document.getElementById('long-break-duration');
const shortBreakButton = document.getElementById('short-break');

function updateBackground() {
    const totalDuration = isBreak ? (isBreak ? longBreakDuration : shortBreakDuration) : workDuration;
    const progress = (totalDuration - currentTime) / totalDuration;
    const red = Math.floor(231 + (255 - 231) * progress);
    const green = Math.floor(76 + (255 - 76) * progress);
    const blue = Math.floor(60 + (255 - 60) * progress);
    document.body.style.backgroundColor = `rgb(${red}, ${green}, ${blue})`;
}

function updateDisplay() {
    const minutes = Math.floor(currentTime / 60);
    const seconds = currentTime % 60;
    timeDisplay.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
    updateBackground();
}

function startPauseTimer() {
    if (isRunning) {
        clearInterval(timer);
        startPauseButton.textContent = '开始';
    } else {
        timer = setInterval(() => {
            if (currentTime > 0) {
                currentTime--;
                updateDisplay();
            } else {
                clearInterval(timer);
                notifyUser();
                if (!isBreak) {
                    sessionCount++;
                    sessionCountDisplay.textContent = sessionCount;
                }
                if (isBreak) {
                    currentTime = workDuration;
                    isBreak = false;
                } else {
                    currentTime = sessionCount % 4 === 0 ? longBreakDuration : shortBreakDuration;
                    isBreak = true;
                }
                updateDisplay();
                startPauseTimer();
            }
        }, 1000);
        startPauseButton.textContent = '暂停';
    }
    isRunning = !isRunning;
}

function resetTimer() {
    clearInterval(timer);
    isRunning = false;
    isBreak = false;
    currentTime = workDuration;
    updateDisplay();
    startPauseButton.textContent = '开始';
    document.body.style.backgroundColor = 'rgb(231, 76, 60)';
}

function notifyUser() {
    if (Notification.permission === 'granted') {
        new Notification('计时结束', {
            body: isBreak ? '休息时间结束，开始工作吧！' : '工作时间结束，休息一下吧！',
        });
    }
}

function requestNotificationPermission() {
    if (Notification.permission !== 'granted') {
        Notification.requestPermission();
    }
}

function updateDurations() {
    shortBreakDuration = parseInt(shortBreakDurationInput.value) * 60;
    longBreakDuration = parseInt(longBreakDurationInput.value) * 60;
    workDuration = parseInt(workDurationInput.value) * 60;
    if (!isRunning) {
        currentTime = workDuration;
        updateDisplay();
    }
}

startPauseButton.addEventListener('click', startPauseTimer);
resetButton.addEventListener('click', resetTimer);
longBreakButton.addEventListener('click', () => {
    if (!isRunning && !isBreak) {
        currentTime = longBreakDuration;
        updateDisplay();
    }
});

shortBreakButton.addEventListener('click', () => {
    if (!isRunning) {
        currentTime = shortBreakDuration;
        updateDisplay();
    }
});

shortBreakDurationInput.addEventListener('change', updateDurations);
longBreakDurationInput.addEventListener('change', updateDurations);

requestNotificationPermission();
updateDisplay(); 